package com.wl.srb.core.controller.api;


import com.wl.common.exception.Assert;
import com.wl.common.result.R;
import com.wl.common.result.ResponseEnum;
import com.wl.srb.core.pojo.entity.UserInfo;
import com.wl.srb.core.pojo.vo.LoginVO;
import com.wl.srb.core.pojo.vo.RegisterVO;
import com.wl.srb.core.pojo.vo.UserInfoVO;
import com.wl.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2021-06-26
 */

@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
@CrossOrigin
public class UserInfoController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserInfoService userInfoService;
    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO){
        String code = registerVO.getCode();
        String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:code" + registerVO.getMobile());
        Assert.equals(code,codeGen, ResponseEnum.CODE_ERROR);
        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login (@RequestBody LoginVO loginVO, HttpServletRequest request){
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        String ip = request.getRemoteAddr();
        UserInfoVO userInfoVO = userInfoService.login(loginVO,ip);
        return R.ok().data("userInfo",userInfoVO).message("登录成功");
    }
}

